﻿#region 命名空间

using System;
using System.Net;
using System.Web.Http;
using System.Web.Mvc;
using Ext.Net;
using Ext.Net.MVC;
using Sharp.DataTransferObject;
using Sharp.Infrastructure;
using Sharp.Infrastructure.WebApiClient;
using Sharp.Portal.ExtNet;
using Sharp.Portal.Mvc;
using Sharp.ServiceContracts;
using Sharp.WebBooter.Areas.Platform.Models;
using PartialViewResult = Ext.Net.MVC.PartialViewResult;

#endregion

namespace Sharp.WebBooter.Areas.Platform.Controllers
{
    [System.Web.Mvc.Authorize]
    [AutomaticTemplate]
    public class OrgLevelController : Controller
    {
        public ActionResult Index(string containerId)
        {
            return new PartialViewResult
            {
                Model = new OrgLevelIndexViewModel(),
                ContainerId = containerId,
                RenderMode = RenderMode.AddTo
            };
        }

        /// <summary>
        ///     分页数据
        /// </summary>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public ActionResult GetPaged(ExtRequestParameters parameters)
        {
            using (var proxy = new WebApiClientServiceProxy<IEnterpriseService>())
            {
                var pageInfo = proxy.Channel.GetOrgLevelPaged(parameters.ToPagingFeature());

                return this.Store(pageInfo.Data, pageInfo.TotalRecords);
            }
        }

        public ActionResult Details(Guid? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            OrgLevelDTO dto;
            using (var proxy = new WebApiClientServiceProxy<IEnterpriseService>())
            {
                dto = proxy.Channel.GetOrgLevelInfo(id.Value);
            }
            if (dto == null)
            {
                return HttpNotFound();
            }

            return new PartialViewResult { Model = dto.To<OrgLevelModel>() };
        }

        public ActionResult Create()
        {
            return new PartialViewResult { Model = new OrgLevelModel() };
        }

        public ActionResult Edit(Guid? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            OrgLevelDTO dto;
            using (var proxy = new WebApiClientServiceProxy<IEnterpriseService>())
            {
                dto = proxy.Channel.GetOrgLevelInfo(id.Value);
            }
            if (dto == null)
            {
                return HttpNotFound();
            }
            return new PartialViewResult { Model = dto.To<OrgLevelModel>() };
        }


        public ActionResult Save(OrgLevelModel model)
        {
            Result res;
            var dto = model.To<OrgLevelDTO>();
            using (var proxy = new WebApiClientServiceProxy<IEnterpriseService>())
            {
                res = proxy.Channel.SaveOrgLevel(dto);
            }

            return this.JsonNet(res);
        }


        public ActionResult Delete(Guid[] ids)
        {
            Result res;
            using (var proxy = new WebApiClientServiceProxy<IEnterpriseService>())
            {
                res = proxy.Channel.DeleteOrgLevel(ids);
            }

            return this.JsonNet(res);
        }
    }
}